﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTTT.DataAccess.DataInf;
using HTTT.Utilities;

namespace HTTT.DataAccess
{
    public class HT_OperatorDAO : IHT_OperatorDAO
    {
        /// <summary>
        /// Gets all HT_Operator
        /// </summary>
        /// <returns></returns>
        /// 

        private static AppEntities dataContext;

        private IRepository<HT_Operator> repository;

        public HT_OperatorDAO()
        {
            dataContext = new AppEntities();
            repository = new RepositoryDAO<HT_Operator>(dataContext);
        }


        public IEnumerable<HT_Operator> GetAllHT_Operator()
        {
            return repository.GetQuery().OrderBy(aobj => aobj.ID);

        }

        public HT_Operator GetHT_OperatorByID(Guid id)
        {
            return repository.Find(obj => obj.ID == id).SingleOrDefault();

        }

        public bool InsertHT_Operator(HT_Operator inHT_Operator)
        {
            if (inHT_Operator == null) return false;
            repository.Add(inHT_Operator);
            return repository.SaveChanges() > 0;
        }

        public bool UpdateHT_Operator(HT_Operator inHT_Operator)
        {
            if (inHT_Operator == null) return false;
            HT_Operator upHT_Operator = repository.First(obj => obj.ID == inHT_Operator.ID);

            if (upHT_Operator == null) return false;

            upHT_Operator.Organize = inHT_Operator.Organize;
            upHT_Operator.Ip = inHT_Operator.Ip;
            upHT_Operator.Speed = inHT_Operator.Speed;
            upHT_Operator.OperatorName = inHT_Operator.OperatorName;
            upHT_Operator.OperatorPhone = inHT_Operator.OperatorPhone;
            upHT_Operator.OperatorEmail= inHT_Operator.OperatorEmail;
            upHT_Operator.OperatorMobile = inHT_Operator.OperatorMobile;
            upHT_Operator.Account = inHT_Operator.Account;
            return repository.SaveChanges() > 0;
        }


        public ReturnValue<bool> DeleteHT_Operator(Guid id)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                HT_Operator objDel = repository.First(obj => obj.ID == id);
                ret = DelHT_Operator(objDel);
            }
            catch (Exception)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;

        }
        public ReturnValue<bool> DelHT_Operator(HT_Operator inHT_Operator)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            if (inHT_Operator == null)
            {
                ret.Msg = "input null";
                return ret;
            }
            try
            {
                repository.Delete(inHT_Operator);
                ret.RetValue = repository.SaveChanges() > 0;
            }
            catch (System.Exception)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }

        public IEnumerable<HT_Operator> SearchHT_Operator(string keyword)
        {
            try
            {
                var r = from m in dataContext.HT_Operator
                        where(m.Organize.Contains(keyword)
                        || m.OperatorName.Contains(keyword)
                        || m.OperatorMobile.Contains(keyword)
                        )
                        select m;
                return r;

            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
